package com.googlecode.connectlet.dictionary;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

import com.googlecode.connectlet.util.ClassPath;
import com.googlecode.connectlet.util.db.DBExecutor;

public class InitDict {
	public static void main(String[] args) throws Exception {
		long t = System.currentTimeMillis();
		DBExecutor db = DBExecutor.getInstance();
		db.source(ClassPath.getInstance("../data/DICT.SQL"));
		System.out.println((System.currentTimeMillis() - t) + "ms");
		FileInputStream fis = new FileInputStream(ClassPath.getInstance("../data/dictionary.gz"));
		BufferedReader br = new BufferedReader(new InputStreamReader(new GZIPInputStream(fis)));
		String str;
		int i = 0;
		while ((str = br.readLine()) != null) {
			int len = str.length();
			if (len < 1 || len > 31) {
				continue;
			}
			String sql = "SELECT id FROM dict_word WHERE word = ?";
			if (db.query(1, sql, str).valid()) {
				continue;
			}
			sql = "INSERT INTO dict_word (len, word) VALUES (?, ?)";
			int[] id = new int[1];
			db.update(id, sql, Integer.valueOf(len), str);
			for (int pos = 0; pos < len; pos ++) {
				sql = "INSERT INTO dict_clp (clp, id) VALUES (?, ?)";
				db.update(sql, DictUtil.CLP(str.charAt(pos), + len, pos), id[0]);
			}
			i = (i + 1) % 1024;
			if (i == 0) {
				System.out.println((System.currentTimeMillis() - t) + "ms " + str);
			}
		}
		br.close();
		System.out.println((System.currentTimeMillis() - t) + "ms");
	}
}